//
//  RetrievePasswordController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/13.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "RetrievePasswordController.h"
#import "PhoneFieldView.h"
#import "VerificationCodeView.h"

@interface RetrievePasswordController ()
/**手机号输入框*/
@property (nonatomic,strong)PhoneFieldView*phoneView;
/**输入密码*/
@property (nonatomic,strong)UITextField*pawField;
/**验证码输入框*/
@property (nonatomic,strong)VerificationCodeView*codeView;
/**协议点击文字*/
@property (nonatomic,strong)AgreementView*agreementView;
/**登录按钮*/
@property (nonatomic,strong)UIButton*loginBtn;

@end

@implementation RetrievePasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName :UIColorFromRGB(0x24B34A), NSFontAttributeName : SYSTEMFONT(19)}];
    self.title = @"找回密码";
    [self createUI];
    [self setNotificaAction];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBarBackgroundAndClearColor];
}

-(void)setNotificaAction{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chaneTextFiel) name:UITextFieldTextDidChangeNotification object:nil];
}
-(void)chaneTextFiel{
    if ([self.phoneView.textFile.text checkPhone] && [self.pawField.text chekePaswd]&& [self.codeView.textFile.text checkeVerificationCode] && self.agreementView.isSelected) {
        self.loginBtn.enabled = YES;
    }else{
        self.loginBtn.enabled = NO;
    }
}
-(void)createUI{
    UIView * oneLine = UIView.new;
    oneLine.backgroundColor = UIColorFromRGB(0xE9EEE7);

    [self.view addSubview:self.phoneView];
    [self.view addSubview:self.pawField];
    [self.view addSubview:oneLine];
    
    [self.view addSubview:self.codeView];
    [self.view addSubview:self.agreementView];
    [self.view addSubview:self.loginBtn];

    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(150);
        make.height.mas_equalTo(40);
    }];
    [self.pawField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(self.phoneView.mas_bottom).offset(10);
        make.height.mas_equalTo(40);
    }];
    [oneLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.pawField);
        make.top.mas_equalTo(self.pawField.mas_bottom);
        make.height.mas_equalTo(0.5);
    }];
    [self.codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(self.pawField.mas_bottom).offset(10);
        make.height.mas_equalTo(40);
    }];
    
    [self.agreementView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(55);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(self.codeView.mas_bottom).offset(60);
    }];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(self.agreementView.mas_bottom).offset(15);
        make.height.mas_equalTo(40);
    }];


}
#pragma mark ---懒加载

-(PhoneFieldView*)phoneView{
    if(!_phoneView){
        _phoneView=[[PhoneFieldView alloc] initWithFrame:CGRectZero];
        _phoneView.textFile.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _phoneView;
}
-(UITextField*)pawField{
    if(!_pawField){
        _pawField=[UITextField new];
        _pawField.placeholder =@"设置密码（6~12位）";
    }
    return _pawField;
}
-(VerificationCodeView*)codeView{
    if(!_codeView){
        _codeView=[[VerificationCodeView alloc] initWithFrame:CGRectZero];
        _codeView.textFile.keyboardType = UIKeyboardTypeNumberPad;
        kWeakSelf(weakSelf);
        _codeView.downBlcok = ^{
            [weakSelf getDownAction:weakSelf.codeView.countdown];
        };

    }
    return _codeView;
}
-(AgreementView*)agreementView{
    if(!_agreementView){
        _agreementView=[[AgreementView alloc] initWithFrame:CGRectZero];
        kWeakSelf(weakSelf);
        _agreementView.touchHandler = ^{
            [weakSelf chaneTextFiel];
        };
    }
    return _agreementView;
}

-(UIButton*)loginBtn{
    if(!_loginBtn){
        _loginBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_loginBtn setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x9ADCAB)] forState:UIControlStateDisabled];
        [_loginBtn setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x24B34A)] forState:UIControlStateNormal];
        _loginBtn.enabled = NO;
        kWeakSelf(weakSelf);
        [_loginBtn addActionHandler:^{
            if ([weakSelf.phoneView.textFile.text checkPhone]) {
                [UserModelManager goToLogin];
            }
        }];
        
    }
    return _loginBtn;
}

@end
